<?php

namespace App\Http\Controllers\Admin;

use App\Models\Rentalhouse;
use App\Services\Admin\ApartmentService;
use App\Services\Admin\AttributeService;
use App\Services\Admin\PropertyService;
use App\Services\Admin\RegionService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RentalhouseController extends Controller
{
    protected $attrService;
    protected $apartService;
    protected $properService;
    protected $regionService;

    public function __construct(AttributeService $attributeService, ApartmentService $apartmentService, PropertyService $propertyService,RegionService $regionService)
    {
        $this->attrService = $attributeService;
        $this->apartService = $apartmentService;
        $this->properService = $propertyService;
        $this->regionService = $regionService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //分类
//        $categorys = Category::with('allChilds')->where('parent_id',0)->orderBy('sort','desc')->get();
        return view('admin.rentalhouse.index');
    }

    public function data(Request $request)
    {
        $model = Rentalhouse::query();

        if ($request->get('title')){
            $model = $model->where('title','like','%'.$request->get('title').'%');
        }
        if ($request->get('community_id')){
            $model = $model->where('community_id',$request->get('community_id'));
        }
        $res = $model->orderBy('created_at','desc')->with('community')->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //区域
        $area = $this->regionService->getArea();
        //朝向
        $categorys = $this->apartService->getApartmentCategory();
        //物业类型
        $pm_type = $this->attrService->getAttributeById(42);
        //装修情况
        $renovation = $this->attrService->getAttributeById(47);
        //房间配套信息
        $supporting = $this->attrService->getAttributeById(52);
        // 特色
        $tags = $this->attrService->getAttributeById(78);
        return view('admin.rentalhouse.create',compact('area','pm_type','categorys','renovation','supporting','tags'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->all();
        //格式化区域 省
        $province = explode(',',$data['province']);
        $data['province'] = $province[0];
        $data['provincename'] = $province[1];
        // 市
        $city = explode(',',$data['region']);
        $data['region'] = $city[0];
        $data['regionname'] = $city[1];
        // 区
        if ($data['area']){
            $area = explode(',',$data['area']);
            $data['area'] = $area[0];
            $data['areaname'] = $area[1];
        }
        // 镇
        if ($data['town']){
            $town = explode(',',$data['town']);
            $data['town_id'] = $town[0];
            $data['town'] = $town[1];
        }
        // 朝向
        if ($data['category_id']){
            $cate = explode(',',$data['category_id']);
            $data['category_id'] = $cate[0];
            $data['direction'] = $cate[1];
        }
        //装修情况
        if ($data['renovation']){
            $renovation = explode(',',$data['renovation']);
            $data['renovation'] = $renovation[0];
            $data['renovation_name'] = $renovation[1];
        }
        $data['supporting'] = isset($data['supporting'])?implode(',',$data['supporting']):'';
        $data['tags'] = isset($data['tags'])?implode(',',$data['tags']):'';
        // 经纬度
        if(!empty($data['map'])){
            $map = explode(',',$data['map']);
            $data['longitude'] = $map[0]??'';
            $data['latitude'] = $map[1]??'';
        }
        // 序列化多图
        if(isset($data['images']) && count($data['images'])){
            $data['images'] = serialize($data['images']);
        }
        $res = Rentalhouse::create($data);
        if($res){
            if($request->input('supporting')){
                $this->properService->updateAttributeMid($request->input('supporting'),$res['id'],'rental');
            }
            if($request->input('tags')){
                $this->properService->updateAttributeMid($request->input('tags'),$res['id'],'rental');
            }
        }
        return redirect(route('admin.rentalhouse'))->with(['status'=>'添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $article = Rentalhouse::findOrFail($id);
        if (!$article){
            return redirect(route('admin.rentalhouse'))->withErrors(['status'=>'小区不存在']);
        }
        //区域
        $area = $this->regionService->getArea();
        //朝向
        $categorys = $this->apartService->getApartmentCategory();
        //物业类型
        $pm_type = $this->attrService->getAttributeById(42);
        //装修情况
        $renovation = $this->attrService->getAttributeById(47);
        //房间配套信息
        $supporting = $this->attrService->getAttributeById(52);
        // 特色
        $tags = $this->attrService->getAttributeById(78);
        return view('admin.rentalhouse.edit',compact('article','area','categorys','renovation','supporting','pm_type','tags'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $article = Rentalhouse::findOrFail($id);
        $data = $request->all();
        //格式化区域 省
        $province = explode(',',$data['province']);
        $data['province'] = $province[0];
        $data['provincename'] = $province[1];
        // 市
        $city = explode(',',$data['region']);
        $data['region'] = $city[0];
        $data['regionname'] = $city[1];
        // 区
        if ($data['area']){
            $area = explode(',',$data['area']);
            $data['area'] = $area[0];
            $data['areaname'] = $area[1];
        }
        // 镇
        if ($data['town']){
            $town = explode(',',$data['town']);
            $data['town_id'] = $town[0];
            $data['town'] = $town[1];
        }
        // 朝向
        if ($data['category_id']){
            $cate = explode(',',$data['category_id']);
            $data['category_id'] = $cate[0];
            $data['direction'] = $cate[1];
        }
        //装修情况
        if ($data['renovation']){
            $renovation = explode(',',$data['renovation']);
            $data['renovation'] = $renovation[0];
            $data['renovation_name'] = $renovation[1];
        }
        $data['supporting'] = isset($data['supporting'])?implode(',',$data['supporting']):'';
        $data['tags'] = isset($data['tags'])?implode(',',$data['tags']):'';
        // 经纬度
        if(!empty($data['map'])){
            $map = explode(',',$data['map']);
            $data['longitude'] = $map[0]??'';
            $data['latitude'] = $map[1]??'';
        }
        // 序列化多图
        if(isset($data['images']) && count($data['images'])){
            $data['images'] = serialize($data['images']);
        }
        // 删除对应属性
        $this->properService->deleteAttributeMid($id,'rental');
        if ($article->update($data)){
            if($request->input('supporting')){
                $this->properService->updateAttributeMid($request->input('supporting'),$id,'rental');
            }
            if($request->input('tags')){
                $this->properService->updateAttributeMid($request->input('tags'),$id,'rental');
            }
            return redirect(route('admin.rentalhouse'))->with(['status'=>'更新成功']);
        }
        return redirect(route('admin.rentalhouse'))->withErrors(['status'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        foreach (Rentalhouse::whereIn('id',$ids)->get() as $model){
            //删除楼盘
            $model->delete();
        }
        return response()->json(['code'=>0,'msg'=>'删除成功']);
    }
}
